from serial_operate import get_stand_sql_data
from tools import deal_time, get_previous_15min_time, get_all_meter_addr, assert_data, make_collect_err_data, \
    modify_centos_current_date_restart

com_name = 'com4'
ip = "192.168.253.113"

# 1、插入前天、昨天、今天的采集数据,前天、昨天误差数据
print("开始第一步骤")

# 设置站端时间为PC当前时间
modify_centos_current_date_restart(ip)
make_collect_err_data(ip, -2, 3, -1, 2)

start, end = get_previous_15min_time()
list_meter_addr = get_all_meter_addr(ip)

start_time = deal_time(start)
end_time = deal_time(end)

# 【检查点4】循环所有表计地址
for meter_addr in list_meter_addr:
    # 【检查点1】请求和应答报文正确
    collect_ere_data = get_stand_sql_data(start_time, end_time, meter_addr, com_name, ip)
    print(collect_ere_data)
    # 【检查点2】采集数据核对，检查频率幅值相位
    assert collect_ere_data.get("hex_recv")[56:64] == "20680200"
    assert_data(4, collect_ere_data.get("message_data")[2] / 10000, collect_ere_data.get("sql_data")[0])
    assert_data(4, collect_ere_data.get("message_data")[3] / 10000, collect_ere_data.get("sql_data")[1])
    assert_data(2, collect_ere_data.get("message_data")[4] / 100, collect_ere_data.get("sql_data")[2])

    # 【检查点3】计算数据核对，比差角差
    assert collect_ere_data.get("hex_recv")[66:74] == "20690200"
    assert_data(3, collect_ere_data.get("message_data")[5] / 1000, collect_ere_data.get("sql_data")[3])
    assert_data(2, collect_ere_data.get("message_data")[6] / 100, collect_ere_data.get("sql_data")[4])
